﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DotSpatial.Controls;
using DotSpatial.Data;

namespace HAGISV2
{
    public partial class frmExportBitmap : Form
    {
        

        Map _map;

        public frmExportBitmap(Map map)
        {
            InitializeComponent();
            
            
            this._map = map;

        }


       

       

        private int _width = 2000;
        private int _height = 2000;

        Bitmap _bitmap;

        double wToh;
        


        private void frmExportBitmap_Load(object sender, EventArgs e)
        {


            //第一步生成预览

            this._bitmap = this._map.SnapShot();


            DotSpatial.Data.InRamImageData imageData = new InRamImageData(this._bitmap);
            imageData.Projection = DotSpatial.Projections.KnownCoordinateSystems.Geographic.World.WGS1984;

            this.pictureBox1.Image = this._bitmap;

            

            //取得map对应的比例

            double orgW = (double)this._map.ViewExtents.Width;
            double orgH = (double)this._map.ViewExtents.Height;

            //计算对应的比例
             wToh = orgW / orgH;
            
            this.numWidth.Value = (decimal)this._width;

            

        }

        private void numWidth_ValueChanged(object sender, EventArgs e)
        {

            ///同步更新height值
            ///

           this ._width = Convert.ToInt32 (this.numWidth.Value);

           this.numHeight.Value = (decimal)(this._width / this.wToh);

            this._height = Convert.ToInt32(this.numHeight.Value);

            //this._bitmap = this.GetPrintBitmap();

            //Graphics g = this.pictureBox1.CreateGraphics();
            //g.Clear(this.pictureBox1.BackColor);
            //g.DrawImage(this._bitmap, 0, 0, this.pictureBox1.Width, this.pictureBox1.Height);
            //g.Dispose();
        }


     

        private void btnSave_Click(object sender, EventArgs e)
        {
            this._bitmap = this.GetPrintBitmap();

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "保存图像";
            sfd.CheckPathExists = true;
            sfd.OverwritePrompt = true;
            sfd.Filter = "Bitmap File(*.bmp)|*.bmp|" + "Gif File(*.gif)|*.gif|" + "JPEG File(*.jpg)|*.jpg|" + "PNG File(*.png)|*.png";
            if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string filename = sfd.FileName;
                string format = filename.Remove(0, filename.Length - 3);
                switch (format)
                {
                    case "bmp":
                        this._bitmap.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
                        break;
                    case "jpg":
                        this._bitmap.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                        break;
                    case "gif":
                        this._bitmap.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Gif);
                        break;
                    case "png":
                        this._bitmap.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Png);
                        break;
                    default :
                        break;

                }
               
            }
        }

        private void numHeight_ValueChanged(object sender, EventArgs e)
        {
           
        }


        /// <summary>
        /// 取得当前设置下的高打印性能图形
        /// </summary>
        /// <returns></returns>

        public Bitmap GetPrintBitmap()
        {

            Bitmap newBitmap = new Bitmap(this._width, this._height);

            //newBitmap.SetResolution(300, 300);

            Graphics g = Graphics.FromImage(newBitmap);

            g.Clear(Color.FromArgb(255, Color.White));        
         
            Rectangle rec = new Rectangle(0, 0, (int)this._width, (int)this._height);

            _map.Print(g, rec, _map.ViewExtents);

            g.Dispose();

            return newBitmap;

 
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            this._bitmap = this.GetPrintBitmap();
            Clipboard.SetImage(this._bitmap);
        }

      
    }
}
